﻿using PBO.Data;
using PBO.Util;
using PBO.Network;
using PBO.RoomClient;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using PBO.Network.Domain;

namespace PBO.Forms.NetworkForm.BattleForm
{
    public class AgentClientForm : BattleClientForm
    {
        private int _agentIdentity;

        private Dictionary<int, TeamData> _teams = new Dictionary<int, TeamData>();
        private int _moveInterval;

        private int _timeCounter;
        private Timer withEventsField__moveTimer;
        private Timer _moveTimer
        {
            get { return withEventsField__moveTimer; }
            set
            {
                if (withEventsField__moveTimer != null)
                {
                    withEventsField__moveTimer.Tick -= Timer_Tick;
                }
                withEventsField__moveTimer = value;
                if (withEventsField__moveTimer != null)
                {
                    withEventsField__moveTimer.Tick += Timer_Tick;
                }
            }
        }
        public AgentClientForm(AgentBattleInfo battleInfo, TeamData team)
            : base(battleInfo.ServerAddress, battleInfo.UserName, team, battleInfo.BattleMode, battleInfo.Position)
        {
            Load += AgentClientForm_Load;
            // 此调用是 Windows 窗体设计器所必需的。
            //InitializeComponent();

            _moveInterval = battleInfo.MoveInterval;
            _agentIdentity = battleInfo.AgentID;
            SetCaption("对战窗口");
        }

        protected override void ConenctServer()
        {
            _client.OnRegistObserver += RegistObserver;
            _client.OnPlayerTimeUp += OnPlayerTimerUp;
            _client.SetPort((int)ServerPort.BattleAgentServerPort);
            base.ConenctServer();
        }
        protected override void OnConnected()
        {
            if ((_client != null))
            {
                _client.Logon(_agentIdentity.ToString(), _battleMode, ModShare.GetVersionInfo());
            }
        }

        private void RegistObserver()
        {
            if (_client == null)
                return;
            BattleInfo info = GetBattleInfo();
            if (info != null)
                _client.SendBattleInfo(info);

            logger.LogAdded += ImageText_Update;
            OnBattleStarted += BattleStarted;
        }

        private void BattleStarted()
        {
            if (_client != null)
                _client.SendBattleInfo(GetBattleInfo());
        }

        private void ImageText_Update(string text, Color color)
        {
            if (_client != null)
                _client.SendBattleSnapshot(GetSnapshot(text, color));
        }

        private void OnPlayerTimerUp(string identity)
        {
            logger.AppendText(string.Format("{0}行动超时.", identity), Color.Red);
        }

        protected override void PlayerMoved(PlayerMove move)
        {
            if ((_moveInterval != 0))
            {
                _timeCounter = 0;
                Invoke(new NetworkLib.VoidFunctionDelegate(_moveTimer.Stop));
            }
            base.PlayerMoved(move);
        }
        protected override void OnWaitMove()
        {
            if ((_moveInterval != 0))
                Invoke(new NetworkLib.VoidFunctionDelegate(_moveTimer.Start));
            base.OnWaitMove();
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            _timeCounter += 1;
            if (_timeCounter == 1)
            {
                logger.AppendText(string.Format("目前还有{0}秒的等待行动时间", Math.Truncate(_moveInterval * 0.75)), Color.Red);
            }
            else if (_timeCounter == 2)
            {
                logger.AppendText(string.Format("目前还有{0}秒的等待行动时间", Math.Truncate(_moveInterval * 0.5)), Color.Red);
            }
            else if (_timeCounter == 3)
            {
                logger.AppendText(string.Format("目前还有{0}秒的等待行动时间,请尽快行动", Math.Truncate(_moveInterval * 0.25)), Color.Red);
            }
            else if (_timeCounter == 4)
            {
                if (_client != null)
                {
                    _client.TimeUp(_identity);
                    logger.AppendText("行动超时,已通知服务器", Color.Red);
                }
            }
        }

        private void AgentClientForm_Load(object sender, System.EventArgs e)
        {
            if ((_moveInterval != 0))
            {
                _moveTimer = new System.Windows.Forms.Timer();
                _moveTimer.Interval = _moveInterval * 250;
                _moveTimer.Stop();
            }
        }

    }
}
